package com.reny.ll.git.mvvm.view

import android.content.Context
import android.view.View
import com.reny.ll.git.mvvm.R
import com.reny.ll.git.mvvm.state.*


/***
 * 默认状态View
 * 想修改部分状态View 继承当前类  重写想修改的View
 */
open class DefaultInitStateView(private val context:Context) :
    MultiStateView.InitStateView {

    private var loadingId = 0

    override fun setLoadingId(layoutId: Int) {
        loadingId = layoutId
    }

    override fun getLoadingId(): Int = loadingId
    override fun getErrorId(): Int = 0
    override fun getNoNetworkId(): Int = 0
    override fun getEmptyId(): Int = 0


    private var mErrorStateView: IStateView? = null
    private var mNoNetworkStateView: IStateView? = null
    private var mEmptyStateView: IStateView? = null
    private var mStateLoadingView: IStateView? = null

    override fun getLoadingView(): View {
        if(mStateLoadingView == null){
            mStateLoadingView = StateLoadingView(context)
        }
        return mStateLoadingView!! as View
    }

    override fun getErrorView(): View {
        if(mErrorStateView == null){
            mErrorStateView = StateErrorView(context)
            /*mErrorStateView?.setImg(R.drawable.state_error)
              mErrorStateView?.setTip("哎呀！出错了！")*/
        }
        return mErrorStateView!! as View
    }

    override fun getNoNetworkView(): View {
        if(mNoNetworkStateView == null) {
            mNoNetworkStateView = StateView(context)
            mNoNetworkStateView?.setImg(R.drawable.state_net_error)
            mNoNetworkStateView?.setTip("无法连接到网络")
            mNoNetworkStateView?.setRetryTip("请检查网络设置后重试，或者直接反馈给我们")
        }
        return mNoNetworkStateView!! as View
    }

    override fun getEmptyView(): View {
        if(mEmptyStateView == null) {
            mEmptyStateView = StateView(context)
            mEmptyStateView?.setImg(R.drawable.state_empty)
            mEmptyStateView?.setTip("暂时没有数据~")
        }
        return mEmptyStateView!! as View
    }


    override fun getStateView(@LoadState state: Int): IStateView? {
        return when (state) {
            STATE_LOADING -> {
                getLoadingView()
                mStateLoadingView
            }
            STATE_ERROR -> {
                getErrorView()
                mErrorStateView
            }
            STATE_NO_NETWORK -> {
                getNoNetworkView()
                mNoNetworkStateView
            }
            STATE_EMPTY -> {
                getEmptyView()
                mEmptyStateView
            }
            else -> {
                null
            }
        }
    }
}